package org.vectrics.recruiter.struts;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.db.DisplayUtils;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;
import org.vectrics.user.web.struts.UserProfileForm;

public class ShowUserEditAction extends org.vectrics.user.web.struts.ShowUserEditAction {

    private transient final Logger log = Logger.getLogger(this.getClass());

    protected UserProfileForm createUserProfileForm() {
        return (new RecruitUserProfileForm());
    }

    @Override
    protected void loadExtendedData(Request request, UserProfileForm profileForm, Person person) {
        log.debug("ShowUserEditAction.loadExtendedData() called - loading candidate data...  profileForm.class = "
                + profileForm.getClass().getName());

        RecruitUserProfileForm form = (RecruitUserProfileForm) profileForm;
        if (person != null) {
            CandidateInfo candidateInfo = RecruitServiceLocator.getRecruitUserService().createCandidateInfoIfNotExist(
                    person);
            form.setOkToContact(candidateInfo.getOkToContact());
            form.setCurrentlyWorking(candidateInfo.getCurrentlyWorking());
            form.setLookingFullTime(candidateInfo.getLookingFullTime());
            form.setLookingPartTime(candidateInfo.getLookingPartTime());
            form.setLookingContract(candidateInfo.getLookingContract());
            form.setLookingPermanent(candidateInfo.getLookingPermanent());

            if (candidateInfo.getTravelPercent() != null)
                form.setTravelPercent(candidateInfo.getTravelPercent().intValue());
            form.setActiveCandidate(candidateInfo.getVoided());
            form.setCareerGoals(candidateInfo.getCareerGoals());
            log.info("Candidate highest education = " + candidateInfo.getHighestEducation());
            if (candidateInfo.getHighestEducation() != null) {
                form.setHighestEducationCode(candidateInfo.getHighestEducation().getCode());
                log.info("Set education code: " + form.getHighestEducationCode());
            }
        }

        // Domain domain = ThreadProperties.getDomain();
        Collection allPersonEducationTypes = RecruitServiceLocator.getPersonService().findAllPersonEducationTypes();
        request.setAttribute("personEducationTypes", DisplayUtils.personEducationTypesToCoded(allPersonEducationTypes,
                request.getLocale()));
    }

    protected Person getPersonToEdit(Request request) {
        log.debug("getUserToEdit() - called");
        Person appUser = null;

        log.debug("getUserToEdit() - getting userId");
        String userId = request.getParameter("userId");
        if (userId == null) {
            userId = request.getParameter("user");
        }
        if (log.isDebugEnabled()) {
            log.debug("getUserToEdit() - userId = " + userId);
        }
        if (userId != null) {
            appUser = CoreServiceLocator.getPersonService().findPersonById(new Long(userId));
        }

        if (appUser == null) {
            try {
                appUser = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(ThreadProperties.getDomain(),
                        request);
            } catch (Throwable ve) {
                log.error("VectricsException finding auth user", ve);
                throw new SystemException("VectricsException finding auth user", ve);
            }
        }

        if (log.isDebugEnabled()) {
            if (appUser == null)
                log.debug("getUserToEdit - user is null");
            else
                log.debug("getUserToEdit - user = " + appUser.getName());
        }
        return (appUser);
    }

}